package com.zygh.policeasset.component;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.zygh.policeasset.mapper.ExcepLogMapper;
import com.zygh.policeasset.mapper.OperaLogMapper;
import com.zygh.policeasset.pojo.ExcepLog;
import com.zygh.policeasset.pojo.OperaLog;
import com.zygh.policeasset.service.TokenService;
import com.zygh.policeasset.utils.JwtTokenUtil;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;

@Aspect
@Component
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/component/OperLogAspect.class */
public class OperLogAspect {
    @Autowired
    OperaLogMapper operaLogMapper;
    @Autowired
    ExcepLogMapper excepLogMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Pointcut("@annotation(com.zygh.policeasset.component.OperLog)")
    public void operLogPoinCut() {
    }

    @Pointcut("execution(* com.zygh.policeasset.controller..*.*(..))")
    public void operExceptionLogPoinCut() {
    }

    @AfterReturning(value = "operLogPoinCut()", returning = "keys")
    public void saveOperLog(JoinPoint joinPoint, Object keys) {
        HttpServletRequest request = (HttpServletRequest) RequestContextHolder.getRequestAttributes().resolveReference("request");
        OperaLog operlog = new OperaLog();
        try {
            Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
            OperLog opLog = (OperLog) method.getAnnotation(OperLog.class);
            if (opLog != null) {
                String operModul = opLog.operModul();
                String operType = opLog.operType();
                String operDesc = opLog.operDesc();
                operlog.setOperModul(operModul);
                operlog.setOperType(operType);
                operlog.setOperDesc(operDesc);
            }
            String className = joinPoint.getTarget().getClass().getName();
            operlog.setOperMethod(className + "." + method.getName());
            operlog.setOperRequParam(JSON.toJSONString(converMap(request.getParameterMap())));
            operlog.setOperRespParam(JSON.toJSONString(keys));
            String token = this.tokenService.getTokenByHeader(request);
            if (token != null) {
                operlog.setOperUserName(this.jwtTokenUtil.getUserNameFromToken(token));
                operlog.setOperUserId(this.jwtTokenUtil.getIdFromToken(token));
            }
            operlog.setOperIp(request.getRemoteAddr());
            operlog.setOperUri(request.getRequestURI());
            operlog.setOperCreateTime(DateUtil.now());
            this.operaLogMapper.insert(operlog);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.toString());
        }
    }

    @AfterThrowing(pointcut = "operExceptionLogPoinCut()", throwing = "e")
    public void saveExceptionLog(JoinPoint joinPoint, Throwable e) {
        HttpServletRequest request = (HttpServletRequest) RequestContextHolder.getRequestAttributes().resolveReference("request");
        ExcepLog excepLog = new ExcepLog();
        try {
            Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
            excepLog.setExcRequParam(JSON.toJSONString(converMap(request.getParameterMap())));
            excepLog.setOperMethod(joinPoint.getTarget().getClass().getName() + "." + method.getName());
            excepLog.setExcName(e.getClass().getName());
            excepLog.setExcMessage(stackTraceToString(e.getClass().getName(), e.getMessage(), e.getStackTrace()));
            String token = this.tokenService.getTokenByHeader(request);
            if (token != null) {
                excepLog.setOperUserName(this.jwtTokenUtil.getUserNameFromToken(token));
                excepLog.setOperUserId(this.jwtTokenUtil.getIdFromToken(token));
            }
            excepLog.setOperUri(request.getRequestURI());
            excepLog.setOperIp(request.getRemoteAddr());
            excepLog.setOperCreateTime(DateUtil.now());
            this.excepLogMapper.insert(excepLog);
        } catch (Exception e2) {
            e.printStackTrace();
            System.out.println(e.toString());
        }
    }

    public Map<String, String> converMap(Map<String, String[]> paramMap) {
        Map<String, String> rtnMap = new HashMap<>();
        for (String key : paramMap.keySet()) {
            if (paramMap.get(key) != null && paramMap.get(key).length > 0) {
                rtnMap.put(key, paramMap.get(key)[0]);
            }
        }
        return rtnMap;
    }

    public String stackTraceToString(String exceptionName, String exceptionMessage, StackTraceElement[] elements) {
        StringBuffer strbuff = new StringBuffer();
        int length = elements.length;
        for (int i = 0; i < length; i++) {
            strbuff.append(elements[i] + "\n");
        }
        return exceptionName + ":" + exceptionMessage + "\n\t" + strbuff.toString();
    }
}